//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "PdeBSM.h"
using namespace Cephei::QL::Methods::Finitedifferences;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Math/TransformedGrid.h>
#include <gen/QL/Methods/Finitedifferences/TridiagonalOperator.h>
#include <gen/QL/Methods/Finitedifferences/PdeSecondOrderParabolic.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::CPdeBSM::CPdeBSM (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process) : CPdeSecondOrderParabolic(CPdeBSM::typeid)
{
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phPdeBSM = NULL;
#endif
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        _ppPdeBSM = new boost::shared_ptr<QuantLib::PdeBSM> (new QuantLib::PdeBSM ( _process ));
        SetPdeSecondOrderParabolic (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeBSM));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::CPdeBSM::CPdeBSM (boost::shared_ptr<QuantLib::PdeBSM>& childNative, Object^ owner) : CPdeSecondOrderParabolic(CPdeBSM::typeid)
{
#ifdef HANDLE
	_phPdeBSM = NULL;
#endif
	_ppPdeBSM = &childNative;
    _ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeBSM));
}
Cephei::QL::Methods::Finitedifferences::CPdeBSM::CPdeBSM (QuantLib::PdeBSM& childNative, Object^ owner) : CPdeSecondOrderParabolic(CPdeBSM::typeid)
{
#ifdef HANDLE
	_phPdeBSM = NULL;
#endif
	_ppPdeBSM = new boost::shared_ptr<QuantLib::PdeBSM> (&childNative);
    _ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeBSM));
    _PdeBSMOwner = owner;
    _PdeSecondOrderParabolicOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::CPdeBSM::CPdeBSM (CPdeBSM^ copy) : CPdeSecondOrderParabolic(CPdeBSM::typeid)
{
#ifdef HANDLE
	_phPdeBSM = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPdeBSM = new boost::shared_ptr<QuantLib::PdeBSM> (copy->GetShared());
        _ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeBSM));
    }
}
Cephei::QL::Methods::Finitedifferences::CPdeBSM::CPdeBSM (System::Type^ t) : CPdeSecondOrderParabolic(CPdeBSM::typeid)
{
#ifdef HANDLE
	_phPdeBSM = NULL;
#endif
	if (!t->IsSubclassOf(CPdeBSM::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::CPdeBSM::CPdeBSM (QuantLib::Handle<QuantLib::PdeBSM>& childNative, Object^ owner)  : CPdeSecondOrderParabolic(CPdeBSM::typeid)
{
	_phPdeBSM = &childNative;
	_ppPdeBSM = &static_cast<boost::shared_ptr<QuantLib::PdeBSM>>(childNative.currentLink());
    _ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeBSM));
    _PdeBSMOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::CPdeBSM::CPdeBSM (QuantLib::Handle<QuantLib::PdeBSM> childNative)  : CPdeSecondOrderParabolic(CPdeBSM::typeid)
{
	_phPdeBSM = &childNative;
	_ppPdeBSM = &static_cast<boost::shared_ptr<QuantLib::PdeBSM>>(childNative.currentLink());
    _ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeBSM));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::CPdeBSM::CPdeBSM (QuantLib::PdeBSM childNative)  : CPdeSecondOrderParabolic(CPdeBSM::typeid)
{
#ifdef HANDLE
	_phPdeBSM = NULL;
#endif
	_ppPdeBSM = new boost::shared_ptr<QuantLib::PdeBSM> (new QuantLib::PdeBSM (childNative));
    _ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (boost::dynamic_pointer_cast<QuantLib::PdeSecondOrderParabolic> (*_ppPdeBSM));
}
#endif

Cephei::QL::Methods::Finitedifferences::CPdeBSM::~CPdeBSM ()
{
    if (_ppPdeBSM != NULL)
    {
	    delete _ppPdeBSM;
        _ppPdeBSM = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::CPdeBSM::!CPdeBSM ()
{
    if (_ppPdeBSM != NULL)
    {
	    delete _ppPdeBSM;
    }
}
QuantLib::PdeBSM& Cephei::QL::Methods::Finitedifferences::CPdeBSM::GetReference ()
{
    if (_ppPdeBSM == NULL) throw gcnew NativeNullException ();
	return **_ppPdeBSM;
}
boost::shared_ptr<QuantLib::PdeBSM>& Cephei::QL::Methods::Finitedifferences::CPdeBSM::GetShared ()
{
    if (_ppPdeBSM == NULL) throw gcnew NativeNullException ();
	return *_ppPdeBSM;
}
QuantLib::PdeBSM* Cephei::QL::Methods::Finitedifferences::CPdeBSM::GetPointer ()
{
    if (_ppPdeBSM == NULL) throw gcnew NativeNullException ();
	return &**_ppPdeBSM;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PdeBSM>& Cephei::QL::Methods::Finitedifferences::CPdeBSM::GetHandle ()
{
	if (_phPdeBSM == NULL)
	{
		_phPdeBSM = new Handle<QuantLib::PdeBSM> (*_ppPdeBSM);
	}
	return *_phPdeBSM;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::CPdeBSM::HasNative () 
{
	return (_ppPdeBSM != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::CPdeBSM::Diffusion (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppPdeBSM)->diffusion ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::CPdeBSM::Discount (Double t, Double prm1)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Real _prm1 = (QuantLib::Real)ValueHelper::Convert (prm1);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppPdeBSM)->discount ( _t,  _prm1 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::CPdeBSM::Drift (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppPdeBSM)->drift ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::IPdeBSM^ Cephei::QL::Methods::Finitedifferences::CPdeBSM_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process)
{
    return gcnew CPdeBSM ( process);
}
